<template>
  <el-card class="box-card">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>个人资料</el-breadcrumb-item>
      <el-breadcrumb-item>修改密码</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form
      ref="UserFormRef"
      :model="UserForm"
      :rules="UserRules"
      label-width="80px"
    >
      <el-form-item label="邮箱">
        <el-input v-model="UserForm.email" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户名">
        <el-input v-model="UserForm.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="UserForm.password" type="password"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary editPwd_btn" @click="onSubmit">修改</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      UserForm: {
        id: JSON.parse(sessionStorage.getItem("userinfo")).id,
        password: "",
        username: JSON.parse(sessionStorage.getItem("userinfo")).username,
        email: JSON.parse(sessionStorage.getItem("userinfo")).email,
      },
      UserRules: {
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  created() {},
  methods: {
    onSubmit() {
      this.$refs.UserFormRef.validate(async (valid) => {
        if (!valid) {
          this.$message.error("请正确的填写表单！");
          return;
        }
        const { data: res } = await this.$http.put("/editpwd", {
          id: this.UserForm.id,
          password: this.UserForm.password,
        });
        if (res.meta.status != 200) {
          this.$message.error(res.meta.msg);
          return;
        }
        localStorage.removeItem("token");
        localStorage.removeItem("userinfo");
        localStorage.removeItem("navname");
        this.$router.push("/");
        this.$message.success("修改成功！");
      });
    },
  },
};
</script>

<style scoped>
.editPwd_btn {
  float: right;
}
</style>
